package org.atf.command;

import java.io.*;

import org.apache.log4j.Logger;

public class ConsoleSimulator
    implements Runnable
{

    public ConsoleSimulator(InputStream is, int type)
    {
        isStop = false;
        this.is = is;
        this.type = type;
    }
    
    public ConsoleSimulator(InputStream is,StringBuffer sb, int type)
    {
        isStop = false;
        this.is = is;
        this.sb=sb;
        this.type = type;
    }
  
    public void run()
    {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(isr);
        String s;
        try
        {
            while(!isStop && (s = reader.readLine()) != null) 
                if(s.length() != 0) {
                	if(type == 0)
                		if(logger != null){
                			logger.trace(s);
                		}
                    else
                    	if(logger != null){
                    		logger.error(s);
                    	}
                	if(sb != null){
                		sb.append(s + "\n");
                	}
                }
                    
            		
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public void stop()
    {
        isStop = true;
    }

    private volatile boolean isStop;
    private InputStream is;
    private StringBuffer sb= null;
    private int type;
    private  static Logger  logger = Logger.getLogger(ConsoleSimulator.class);
}
